﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverTile.Xaml
{
    public class SpriteFrame : FrameworkElement
    {

        public SpriteFrame()
        { }

        public static readonly DependencyProperty SourceProperty = DependencyProperty.RegisterAttached("Source", typeof(string), typeof(SpriteFrame), null);
        public string Source
        {
            get { return (string)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static readonly DependencyProperty OriginXProperty = DependencyProperty.RegisterAttached("OriginX", typeof(int), typeof(SpriteFrame), null);
        public Int32 OriginX
        {
            get { return (Int32)GetValue(OriginXProperty); }
            set { SetValue(OriginXProperty, value); }
        }

        public static readonly DependencyProperty OriginYProperty = DependencyProperty.RegisterAttached("OriginY", typeof(int), typeof(SpriteFrame), null);
        public Int32 OriginY
        {
            get { return (Int32)GetValue(OriginYProperty); }
            set { SetValue(OriginYProperty, value); }
        }


        public static readonly DependencyProperty TopProperty = DependencyProperty.RegisterAttached("Top", typeof(int), typeof(SpriteFrame), null);
        public Int32 Top
        {
            get { return (Int32)GetValue(TopProperty); }
            set { SetValue(TopProperty, value); }
        }

        public static readonly DependencyProperty BottomProperty = DependencyProperty.RegisterAttached("Bottom", typeof(int), typeof(SpriteFrame), null);
        public Int32 Bottom
        {
            get { return (Int32)GetValue(BottomProperty); }
            set { SetValue(BottomProperty, value); }
        }

        public static readonly DependencyProperty LeftProperty = DependencyProperty.RegisterAttached("Left", typeof(int), typeof(SpriteFrame), null);
        public Int32 Left
        {
            get { return (Int32)GetValue(LeftProperty); }
            set { SetValue(LeftProperty, value); }
        }

        public static readonly DependencyProperty RightProperty = DependencyProperty.RegisterAttached("Right", typeof(int), typeof(SpriteFrame), null);
        public Int32 Right
        {
            get { return (Int32)GetValue(RightProperty); }
            set { SetValue(RightProperty, value); }
        }

        public static readonly DependencyProperty BackgroundBoundsProperty =
            DependencyProperty.RegisterAttached("BackgroundBounds", typeof(Rect), typeof(SpriteFrame), null);
        public Rect BackgroundBounds
        {
            get { return (Rect)GetValue(BackgroundBoundsProperty); }
            set { SetValue(BackgroundBoundsProperty, value); }
        }

        //public static readonly DependencyProperty BlockingProperty = DependencyProperty.Register("Blocking", typeof(Boolean), typeof(SpriteFrame), null);
        public Boolean Blocking
        {
            get;
            set;
        }

        private IFrame _frame;
        public IFrame Frame
        {
            get
            {
                if (_frame == null)
                {
                    _frame = new Frame(Source)
                    {
                        OriginX = this.OriginX,
                        OriginY = this.OriginY,
                        Bounds = new Rect(new Point(Left + OriginX, Top + OriginY), new Point(Right + OriginX, Bottom + OriginY)),
                        BackgroundBounds = this.BackgroundBounds
                    };
                }
                return _frame;
            }
        }

    }
}
